<template>
  <el-autocomplete
      v-model="text"
      :fetch-suggestions="querySearch"
      class="inline-input"
      :placeholder="this.label"
      @change="toParent">
  </el-autocomplete>
</template>

<script>
export default {
  name: "SearchInput",
  //data传入的数组 keyword要模糊搜索的关键字，输入框里面的提示
  props: ['data', 'keyword', 'label'],
  data() {
    return {
      text: ''
    }
  },
  methods: {
    toParent(value) {
      this.$emit("getText",value)
    },
    querySearch(queryString, cb) {
      if (queryString === '') {
        cb(this.createFilter(this.data))
      } else {
        const a = []
        this.data.forEach((item) => {
          if (item[this.keyword].indexOf(queryString) !== -1) {
            a.push({
              value: item[this.keyword]
            })
          }
          cb(a)
        })
      }
    },
    createFilter(data) {
      const a = []
      data.forEach((item) => {
        a.push({
          value: item[this.keyword]
        })
      })
      return a;
    }
  }
}
</script>

<style scoped>

</style>